package com.clc.service.impl;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;

import org.apache.commons.httpclient.HttpException;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clc.comm.util.HTTPInterfaceUtil;
import com.clc.comm.util.PropertiesUtil;
import com.clc.service.IWeatherService;
@Service("weatherService")
public class WeatherServiceImpl implements IWeatherService {
	@Autowired
	public PropertiesUtil propertiesUtil;
	@Override
	public String getLocalWeather() throws HttpException, IOException, JSONException {
		String cityCode = getCityCode();
		String body = geWrather(propertiesUtil.get(cityCode));
		return body;
	}
	@Override
	public String getLocalAQI() throws HttpException, IOException, JSONException {
		String cityCode = getCityCode();
		String body = getAQI(cityCode);
		return body;
	}
	
	private String getCityCode() throws HttpException, IOException, JSONException{
		HashMap<Byte, Object> param= new HashMap<Byte, Object>();
		param.put(HTTPInterfaceUtil.REQUEST_METHOD_KEY, HTTPInterfaceUtil.REQUEST_GET);
		param.put(HTTPInterfaceUtil.RETRY_HANDLER_COUNT_KEY, 3);
		param.put(HTTPInterfaceUtil.PROXY_HOST_KEY, "10.101.1.6");
		param.put(HTTPInterfaceUtil.PROXY_PORT_KEY, 80);
		param.put(HTTPInterfaceUtil.PROXY_USERNAME_KEY, "_SSO_T_00105");
		param.put(HTTPInterfaceUtil.PROXY_PASSWORD_KEY, "t00105@sso");
		//param.put(HTTPInterfaceUtil.REQUEST_TIMEOUT, 5);
		String url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=JSON";
		String rel = HTTPInterfaceUtil.request(url, param);
		JSONObject dpool = new JSONObject(rel);
		return (String) dpool.get("city");
	}
	
	private String geWrather(String code) throws HttpException, IOException, JSONException{
		HashMap<Byte, Object> param= new HashMap<Byte, Object>();
		param.put(HTTPInterfaceUtil.REQUEST_METHOD_KEY, HTTPInterfaceUtil.REQUEST_GET);
		param.put(HTTPInterfaceUtil.RETRY_HANDLER_COUNT_KEY, 3);
		param.put(HTTPInterfaceUtil.PROXY_HOST_KEY, "10.101.1.6");
		param.put(HTTPInterfaceUtil.PROXY_PORT_KEY, 80);
		param.put(HTTPInterfaceUtil.PROXY_USERNAME_KEY, "_SSO_T_00105");
		param.put(HTTPInterfaceUtil.PROXY_PASSWORD_KEY, "t00105@sso");
		//param.put(HTTPInterfaceUtil.REQUEST_TIMEOUT, 5);
		String url = "http://m.weather.com.cn/data/"+code+".html";
		String rel = HTTPInterfaceUtil.request(url, param);
		return rel;
	}
	
	private String getAQI(String code)throws HttpException, IOException, JSONException{
		HashMap<Byte, Object> param= new HashMap<Byte, Object>();
		param.put(HTTPInterfaceUtil.REQUEST_METHOD_KEY, HTTPInterfaceUtil.REQUEST_GET);
		param.put(HTTPInterfaceUtil.RETRY_HANDLER_COUNT_KEY, 3);
		param.put(HTTPInterfaceUtil.PROXY_HOST_KEY, "10.101.1.6");
		param.put(HTTPInterfaceUtil.PROXY_PORT_KEY, 80);
		param.put(HTTPInterfaceUtil.PROXY_USERNAME_KEY, "_SSO_T_00105");
		param.put(HTTPInterfaceUtil.PROXY_PASSWORD_KEY, "t00105@sso");
		//param.put(HTTPInterfaceUtil.REQUEST_TIMEOUT, 5);
		String url = "http://www.pm25.in/api/querys/pm2_5.json?city="+URLEncoder.encode(code,"UTF-8")+"&token=5j1znBVAsnSf5xQyNQyq&stations=no";
		String rel = HTTPInterfaceUtil.request(url, param);
		return rel;
	}

	
}
